home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / clean / sun3.lha / Sun3 / seqdemos / pascal.icl < prev    next >
Text File  |  1992-08-07  |  2KB  |  103 lines

  1. MODULE pascal;
  2.  
  3. <<
  4. The Triangle of Pascal.
  5.  
  6. The result of this program is a real triangle of Pascal of height
  7. Height, not just a list representing such a triangle:
  8.  
  9.         1
  10.        1 1
  11.       1 2 1
  12.      1 3 3 1
  13.     1 4 6 4 1    etc.
  14. >>
  15.  
  16. IMPORT deltaI, deltaIO;
  17.  
  18.  
  19. TYPE
  20.  
  21. <<  A row of the triangle is represented by a list of integers,
  22.     the triangle as a list of rows:
  23. >>
  24. ::  Row      -> [INT];
  25. ::  Triangle -> [Row];
  26.  
  27.  
  28. MACRO
  29.  
  30. ==  Some constants
  31.  
  32.     NrRows -> 18;   ==  Number of rows to be shown.
  33.     Middle -> 40;   ==  The middle of a 80 character line.
  34.     
  35.  
  36. RULE
  37.  
  38. ==  Miscellaneous functions
  39.     
  40. ::  AddList [INT]   [INT]   -> [INT];
  41.     AddList [a|x]   [b|y]   -> [+ a b | AddList x y];
  42.     AddList []      []      -> [];
  43.  
  44. ::  Append [INT]    INT -> [INT];
  45.     Append [a|r]    x   -> [a | Append r x];
  46.     Append []       x   -> [x];
  47.  
  48. ::  Take INT [x] -> [x];
  49.     Take 0 list  -> [];
  50.     Take n [h|t] -> [h | Take (-- n) t];
  51.     Take n []    -> [];
  52.  
  53. ::  FPutInt INT FILE -> FILE;
  54.     FPutInt n out    -> FPutS (ITOS n) out;
  55.  
  56. ::  NrOfDigits INT  -> INT;
  57.     NrOfDigits 0    -> 0;
  58.     NrOfDigits n    -> ++ (NrOfDigits (/ n 10));
  59.  
  60.  
  61. ==  Calculating the Triangle.
  62.  
  63. ::  Pascal -> Triangle;
  64.     Pascal -> p: [[1] | MapNext p];
  65.  
  66. ::  MapNext Triangle -> Triangle;
  67.     MapNext [a|r]    -> [Next a | MapNext r];
  68.     MapNext []       -> [];
  69.  
  70. ::  Next Row -> Row;
  71.     Next x -> AddList [0|x] (Append x 0);
  72.  
  73.  
  74. ==  Formatting the list representing the triangle as a real triangle.
  75.  
  76. ::  FormatRows Triangle FILE -> FILE;
  77.     FormatRows [f|r] out -> FormatRows r (FPutC '\n' (FormatRow f out));
  78.     FormatRows []    out -> out;
  79.  
  80. ::  FormatRow Row FILE -> FILE;
  81.     FormatRow row out
  82.         -> FormatElems row (Spaces (- Middle (/ (Length row) 2)) out);
  83.  
  84. ::  FormatElems Row FILE -> FILE;
  85.     FormatElems [f|r] out -> FormatElems r (FPutInt f (FPutC ' ' out));
  86.     FormatElems []    out -> out;
  87.  
  88. ::  Spaces INT FILE -> FILE;
  89.     Spaces n out -> out                          , IF <= n 0
  90.                  -> Spaces (-- n) (FPutC ' ' out);
  91.  
  92. ::  Length Row -> INT;
  93.     Length [f|r] -> ++ (+ (NrOfDigits f) (Length r));
  94.     Length []    -> -1;
  95.  
  96.  
  97. <<  The Start rule: The first NrRows rows of the (infinite) triangle
  98.     returned by Pascal are taken and shown on the screen (StdIO) as a
  99.     triangle by means of FormatRows.
  100. >>
  101. ::  Start -> FILE;
  102.     Start -> FormatRows (Take NrRows Pascal) StdIO;
  103.